// src/logger.js
import fs from 'fs';
import path from 'path';

const logDir = path.resolve(process.cwd(), 'logs');
if (!fs.existsSync(logDir)) {
  fs.mkdirSync(logDir);
}

const logFile = path.join(logDir, `${new Date().toISOString().slice(0, 10)}.log`);

function getLocalTimeString() {
  const now = new Date();
  const pad = n => n.toString().padStart(2, '0');
  return `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} ${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`;
}

function formatMsg(level, msg) {
  return `[${getLocalTimeString()}] [${level}] ${msg}`;
}

export function logInfo(msg) {
  const line = formatMsg('INFO', msg);
  console.log(line);
  fs.appendFileSync(logFile, line + '\n');
}

export function logError(msg) {
  const line = formatMsg('ERROR', msg);
  console.error(line);
  fs.appendFileSync(logFile, line + '\n');
}

export function logWarn(msg) {
  const line = formatMsg('WARN', msg);
  console.warn(line);
  fs.appendFileSync(logFile, line + '\n');
}
